/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package businesscard.DAO;

import bc.entity.Lienhe;
import bc.util.HibernateUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author THICH HOA DONG TIEN
 */
public class ThongkeDAO {

    private Session session;

    public ThongkeDAO() {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        // this.session = factory.getCurrentSession();
        this.session = factory.openSession();
    }

    //thong ke Lien he theo quy
    public List<Lienhe> TKlienheByQuy(String Quy) {
        session.beginTransaction().begin();
        String Sql = "from Lienhe where month(ngay) in ? and year(now()) GROUP BY idlienhe";
        try {
            Query query = session.createQuery(Sql);
            query.setString(0, "%" + Quy + "%");
            List result = query.list();
            session.flush();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();

        }

        return null;

    }

    public Date stringMonthToDate(String Month) {

        return null;
    }

    public List<Lienhe> TKLienheByThang(String month, String year) {
        session.beginTransaction().begin();
        String sql = "from Lienhe where month(ngay)=?and year(ngay)=?";

        try {
            Query query = session.createQuery(sql);
            query.setString(0, month);
            query.setString(1, year);

            List<Lienhe> result = query.list();
            session.flush();
            return result;

        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws ParseException {
        ThongkeDAO tkdao = new ThongkeDAO();
        List<Lienhe> a = tkdao.TKLienheByThang("12", "2013");
        if(a.isEmpty())
        {
            System.out.println("khong co");
        }
        for (int i = 0; i < a.size(); i++) {
            System.out.println(a.get(i).getTen());
        }
    }

}
